﻿<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>修改密码</title>
    <link href="${pageContext.request.contextPath}/resources/bootstrap5/css/bootstrap.min.css" rel="stylesheet">
    <!-- Bootstrap Icons - 本地版本 -->
    <link href="${pageContext.request.contextPath}/resources/bootstrap-icons/1.11.3/bootstrap-icons.css" rel="stylesheet">
    <!-- Bootstrap Icons - 备用CDN -->
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap-icons/1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
    <!-- 第二个备用CDN -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css">
    <style>
        body {
            font-family: 'Segoe UI', 'Microsoft YaHei', Arial, sans-serif;
            background: #f5f7fa;
            margin: 0;
            padding: 0;
        }
        .header {
            background: #2196f3;
            color: #fff;
            padding: 0 32px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: 56px;
            box-shadow: 0 2px 8px rgba(33,150,243,0.08);
            position: fixed;
            width: 100%;
            top: 0;
            z-index: 1000;
        }
        .header-left {
            display: flex;
            align-items: center;
        }
        .header-logo {
            width: 36px;
            height: 36px;
            margin-right: 12px;
        }
        .header-title {
            font-size: 1.4rem;
            font-weight: bold;
            letter-spacing: 1px;
        }
        .header-nav {
            margin-left: 32px;
            font-size: 1rem;
        }
        .header-nav a {
            color: #fff;
            text-decoration: none;
            margin-right: 16px;
            opacity: 0.85;
        }
        .header-nav a.active, .header-nav a:hover {
            opacity: 1;
            font-weight: bold;
        }

        .sidebar {
            position: fixed;
            top: 56px;
            left: 0;
            bottom: 0;
            width: 220px;
            background: #fff;
            box-shadow: 2px 0 8px rgba(33,150,243,0.06);
            overflow-y: auto;
            z-index: 100;
            padding-top: 16px;
        }
        .sidebar-menu {
            list-style: none;
            padding: 0;
            margin: 0;
        }
        .sidebar-menu > li {
            margin-bottom: 4px;
        }
        .sidebar-menu > li > a {
            display: flex;
            align-items: center;
            padding: 10px 24px;
            color: #333;
            text-decoration: none;
            border-radius: 8px 0 0 8px;
            transition: background 0.2s, color 0.2s;
            font-size: 1rem;
        }
        .sidebar-menu > li > a:hover, .sidebar-menu > li > a.active {
            background: #e3f2fd;
            color: #2196f3;
            font-weight: bold;
        }
        .sidebar-menu > li > a i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
            font-size: 1.1rem;
        }
        .sidebar-submenu {
            list-style: none;
            padding-left: 40px;
            max-height: 0;
            overflow: hidden;
            transition: max-height 0.3s ease;
        }
        .sidebar-menu > li.open .sidebar-submenu {
            max-height: 500px;
        }
        .sidebar-submenu > li > a {
            display: block;
            padding: 7px 0;
            color: #666;
            text-decoration: none;
            transition: color 0.2s;
            font-size: 0.98rem;
        }
        .sidebar-submenu > li > a:hover, .sidebar-submenu > li > a.active {
            color: #2196f3;
            font-weight: 500;
        }

        .content {
            margin-left: 240px;
            padding: 80px 32px 32px;
        }

        .password-form-container {
            max-width: 500px;
            margin: 0 auto;
        }

        .card {
            background: #fff;
            border-radius: 12px;
            box-shadow: 0 2px 12px rgba(33,150,243,0.07);
            margin-bottom: 24px;
            border: none;
        }
        .card-body {
            padding: 30px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            font-weight: 600;
            color: #333;
            margin-bottom: 8px;
        }

        .form-control {
            border-radius: 8px;
            border: 1px solid #e3e3e3;
            padding: 12px 16px;
            font-size: 1rem;
            transition: border-color 0.2s, box-shadow 0.2s;
        }

        .form-control:focus {
            border-color: #2196f3;
            box-shadow: 0 0 0 0.2rem rgba(33,150,243,0.25);
        }

        .btn-primary {
            background: #2196f3;
            border: none;
            border-radius: 8px;
            padding: 12px 24px;
            font-weight: 500;
            transition: all 0.2s;
        }

        .btn-primary:hover {
            background: #1976d2;
            transform: translateY(-1px);
        }

        .btn-secondary {
            border-radius: 8px;
            padding: 12px 24px;
            font-weight: 500;
        }

        .alert {
            border-radius: 8px;
            border: none;
            font-size: 0.95rem;
        }

        .alert-success {
            background: #e8f5e8;
            color: #2e7d32;
        }

        .alert-danger {
            background: #ffebee;
            color: #c62828;
        }

        .password-requirements {
            background: #f8f9fa;
            border-radius: 8px;
            padding: 15px;
            margin-top: 10px;
            font-size: 0.9rem;
            color: #666;
        }

        .password-requirements ul {
            margin: 0;
            padding-left: 20px;
        }

        .password-requirements li {
            margin-bottom: 5px;
        }

        /* 用户名下拉菜单样式 */
        .header-user {
            display: flex;
            align-items: center;
            position: relative;
        }
        .header-user-avatar {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            margin-right: 8px;
            object-fit: cover;
            background: #fff;
            cursor: pointer;
            transition: transform 0.2s;
        }
        .header-user-avatar:hover {
            transform: scale(1.1);
        }
        .header-user-name {
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            font-family: 'Microsoft YaHei', Arial, sans-serif;
        }
        .user-dropdown {
            position: absolute;
            top: 100%;
            right: 0;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
            min-width: 160px;
            z-index: 1001;
            opacity: 0;
            visibility: hidden;
            transform: translateY(-10px);
            transition: all 0.3s ease;
            margin-top: 8px;
        }
        .user-dropdown.show {
            opacity: 1;
            visibility: visible;
            transform: translateY(0);
        }
        .user-dropdown::before {
            content: '';
            position: absolute;
            top: -6px;
            right: 20px;
            width: 12px;
            height: 12px;
            background: #fff;
            transform: rotate(45deg);
            box-shadow: -2px -2px 4px rgba(0,0,0,0.1);
        }
        .user-dropdown-item {
            display: flex;
            align-items: center;
            padding: 12px 16px;
            color: #333;
            text-decoration: none;
            transition: background 0.2s;
            border-bottom: 1px solid #f0f0f0;
            font-family: 'Microsoft YaHei', Arial, sans-serif;
        }
        .user-dropdown-item:last-child {
            border-bottom: none;
        }
        .user-dropdown-item:hover {
            background: #f8f9fa;
            color: #2196f3;
        }
        .user-dropdown-item i {
            margin-right: 8px;
            width: 16px;
            text-align: center;
        }

        @media (max-width: 900px) {
            .content {
                margin-left: 0;
                padding: 80px 15px 15px;
            }
            .sidebar {
                width: 100%;
                position: static;
                box-shadow: none;
            }
            .password-form-container {
                max-width: 100%;
            }
        }
    </style>
</head>
<body>
<!-- 头部导航栏 -->
<div class="header">
    <div class="header-left">
        <img src="${pageContext.request.contextPath}/resources/images/logo.png" alt="Logo" class="header-logo">
        <span class="header-title">教务管理系统</span>
        <div class="header-nav">
            <a href="${pageContext.request.contextPath}/student/home">首页</a>
            <span style="opacity:0.5;">/</span>
            <a href="#" class="active">修改密码</a>
        </div>
    </div>

    <!-- 用户名下拉菜单 -->
    <div class="header-user" id="userDropdownContainer">
        <img src="${pageContext.request.contextPath}/resources/images/baimao.png" class="header-user-avatar" alt="用户头像" id="userAvatar">
        <span class="header-user-name" id="userName">
            <c:choose>
                <c:when test="${not empty sessionScope.loggedInUser and not empty sessionScope.loggedInUser.realName}">
                    ${sessionScope.loggedInUser.realName}
                </c:when>
                <c:when test="${not empty sessionScope.loggedInUser and not empty sessionScope.loggedInUser.username}">
                    ${sessionScope.loggedInUser.username}
                </c:when>
                <c:otherwise>
                    学生
                </c:otherwise>
            </c:choose>
        </span>
        <div class="user-dropdown" id="userDropdown">
            <a href="#" class="user-dropdown-item" onclick="showProfileModal()">
                <i class="bi bi-person-circle"></i>
                个人信息
            </a>
            <a href="${pageContext.request.contextPath}/student/changePassword" class="user-dropdown-item">
                <i class="bi bi-key"></i>
                修改密码
            </a>
            <a href="${pageContext.request.contextPath}/user/logout" class="user-dropdown-item">
                <i class="bi bi-box-arrow-right"></i>
                退出系统
            </a>
        </div>
    </div>
</div>

<!-- 侧边栏 -->
<div class="sidebar">
    <ul class="sidebar-menu">
        <li>
            <a href="${pageContext.request.contextPath}/student/home" class="active">
                <i class="bi bi-house-door"></i> 学生首页
            </a>
        </li>
        <li>
            <a href="#" class="has-submenu" onclick="toggleSubmenu(this)">
                <i class="bi bi-megaphone"></i> 通知公告
                <i class="bi bi-chevron-down ms-auto"></i>
            </a>
            <ul class="sidebar-submenu">
                <li><a href="${pageContext.request.contextPath}/student/notifications/academic">教务通知</a></li>
                <li><a href="${pageContext.request.contextPath}/student/notifications/exam">考试安排</a></li>
                <li><a href="${pageContext.request.contextPath}/student/notifications/classroom">教室安排</a></li>
            </ul>
        </li>
        <li>
            <a href="#" class="has-submenu" onclick="toggleSubmenu(this)">
                <i class="bi bi-building"></i> 院系管理
                <i class="bi bi-chevron-down ms-auto"></i>
            </a>
            <ul class="sidebar-submenu">
                <li><a href="${pageContext.request.contextPath}/student/college">学院管理</a></li>
                <li><a href="${pageContext.request.contextPath}/student/major">专业管理</a></li>
                <li><a href="${pageContext.request.contextPath}/student/class">班级管理</a></li>
            </ul>
        </li>
        <li>
            <a href="#" class="has-submenu" onclick="toggleSubmenu(this)">
                <i class="bi bi-book"></i> 课程管理
                <i class="bi bi-chevron-down ms-auto"></i>
            </a>
            <ul class="sidebar-submenu">
                <li><a href="${pageContext.request.contextPath}/student/courses">课程管理</a></li>
                <li><a href="${pageContext.request.contextPath}/student/my-courses">我的
                    课程</a></li>
                <li><a href="${pageContext.request.contextPath}/student/schedule">我的课表</a></li>
                <li><a href="${pageContext.request.contextPath}/student/grades">成绩管理</a></li>
                <li><a href="${pageContext.request.contextPath}/student/evaluation">课程评价</a></li>
            </ul>
        </li>
        <li>
            <a href="#" class="has-submenu" onclick="toggleSubmenu(this)">
                <i class="bi bi-clipboard-check"></i> 教学管理
                <i class="bi bi-chevron-down ms-auto"></i>
            </a>
            <ul class="sidebar-submenu">
                <li><a href="${pageContext.request.contextPath}/student/leave">请假管理</a></li>
                <li><a href="${pageContext.request.contextPath}/student/homework">作业提交</a></li>
                <li><a href="${pageContext.request.contextPath}/student/attendance">考勤管理</a></li>
            </ul>
        </li>
    </ul>
</div>

<!-- 主内容区 -->
<div class="content">
    <div class="password-form-container">
        <div class="card">
            <div class="card-body">
                <div class="text-center mb-4">
                    <i class="bi bi-shield-lock" style="font-size: 3rem; color: #2196f3;"></i>
                    <h4 class="mt-3 mb-2">修改密码</h4>
                    <p class="text-muted">为了您的账户安全，请定期更新密码</p>
                </div>

                <!-- 消息提示区域 -->
                <div id="messageArea"></div>

                <form id="changePasswordForm">
                    <div class="form-group">
                        <label for="oldPassword" class="form-label">
                            <i class="bi bi-lock me-2"></i>原密码
                        </label>
                        <input type="password" class="form-control" id="oldPassword" name="oldPassword"
                               placeholder="请输入原密码" required>
                    </div>

                    <div class="form-group">
                        <label for="newPassword" class="form-label">
                            <i class="bi bi-key me-2"></i>新密码
                        </label>
                        <input type="password" class="form-control" id="newPassword" name="newPassword"
                               placeholder="请输入新密码" required>
                        <div class="password-requirements">
                            <strong>密码要求：</strong>
                            <ul>
                                <li>至少6个字符</li>
                                <li>建议包含字母和数字</li>
                                <li>不要使用过于简单的密码</li>
                            </ul>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="confirmPassword" class="form-label">
                            <i class="bi bi-check-circle me-2"></i>确认新密码
                        </label>
                        <input type="password" class="form-control" id="confirmPassword" name="confirmPassword"
                               placeholder="请再次输入新密码" required>
                    </div>

                    <div class="d-grid gap-2 d-md-flex justify-content-md-center mt-4">
                        <button type="submit" class="btn btn-primary me-md-2" id="submitBtn">
                            <i class="bi bi-check-lg me-2"></i>确认修改
                        </button>
                        <a href="${pageContext.request.contextPath}/student/home" class="btn btn-secondary">
                            <i class="bi bi-arrow-left me-2"></i>返回首页
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- 个人信息模态框 -->
<div class="modal fade" id="profileModal" tabindex="-1" aria-labelledby="profileModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="border-radius: 12px;">
            <div class="modal-header" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 12px 12px 0 0;">
                <h5 class="modal-title" id="profileModalLabel">
                    <i class="bi bi-person-circle"></i> 个人信息
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body" style="padding: 30px;">
                <div class="row mb-4">
                    <div class="col-12 text-center">
                        <div style="width: 80px; height: 80px; border-radius: 50%; margin: 0 auto 15px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); display: flex; align-items: center; justify-content: center; color: white; font-size: 2rem;">
                            <i class="bi bi-person-circle"></i>
                        </div>
                        <h5 class="mb-1">学生个人信息</h5>
                        <p class="text-muted mb-0">详细信息</p>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-6 mb-3">
                        <div style="padding: 15px; background: #f8f9fa; border-radius: 8px; border-left: 4px solid #667eea;">
                            <strong style="color: #667eea;"><i class="bi bi-person me-2"></i>真实姓名</strong>
                            <div class="mt-1">
                                <c:choose>
                                    <c:when test="${not empty sessionScope.loggedInUser.realName}">
                                        ${sessionScope.loggedInUser.realName}
                                    </c:when>
                                    <c:when test="${not empty sessionScope.loggedInUser.username}">
                                        ${sessionScope.loggedInUser.username}
                                    </c:when>
                                    <c:otherwise>
                                        <span class="text-muted">未设置</span>
                                    </c:otherwise>
                                </c:choose>
                            </div>
                        </div>
                    </div>

                    <div class="col-md-6 mb-3">
                        <div style="padding: 15px; background: #f8f9fa; border-radius: 8px; border-left: 4px solid #667eea;">
                            <strong style="color: #667eea;"><i class="bi bi-person-badge me-2"></i>用户名</strong>
                            <div class="mt-1">
                                <c:choose>
                                    <c:when test="${not empty sessionScope.loggedInUser.username}">
                                        ${sessionScope.loggedInUser.username}
                                    </c:when>
                                    <c:otherwise>
                                        <span class="text-muted">未设置</span>
                                    </c:otherwise>
                                </c:choose>
                            </div>
                        </div>
                    </div>

                    <div class="col-12 mb-3">
                        <div style="padding: 15px; background: #f8f9fa; border-radius: 8px; border-left: 4px solid #667eea;">
                            <strong style="color: #667eea;"><i class="bi bi-info-circle me-2"></i>系统提示</strong>
                            <div class="mt-1">
                                <p class="mb-0 text-muted">完整的个人信息请在学生首页查看，这里仅显示基本用户信息。</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                <a href="${pageContext.request.contextPath}/student/home" class="btn" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white;">
                    <i class="bi bi-house-door me-1"></i>返回首页
                </a>
            </div>
        </div>
    </div>
</div>

<script src="${pageContext.request.contextPath}/resources/bootstrap5/js/bootstrap.bundle.min.js"></script>
<script>
    // 用户名下拉菜单
    document.getElementById('userDropdownContainer').addEventListener('click', function(e) {
        e.stopPropagation();
        const dropdown = document.getElementById('userDropdown');
        dropdown.classList.toggle('show');
    });

    // 点击其他地方关闭下拉菜单
    document.addEventListener('click', function() {
        const dropdown = document.getElementById('userDropdown');
        dropdown.classList.remove('show');
    });

    // 侧边栏子菜单切换
    function toggleSubmenu(element) {
        const parent = element.parentElement;

        // 关闭其他打开的子菜单
        document.querySelectorAll('.sidebar-menu > li').forEach(li => {
            if (li !== parent) {
                li.classList.remove('open');
            }
        });

        // 切换当前子菜单
        parent.classList.toggle('open');
    }

    // 显示消息
    function showMessage(message, type) {
        const messageArea = document.getElementById('messageArea');
        const iconClass = type === 'success' ? 'check-circle' : 'exclamation-triangle';
        messageArea.innerHTML = `
            <div class="alert alert-${type} alert-dismissible fade show" role="alert">
                <i class="bi bi-${iconClass} me-2"></i>
                ${message}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        `;
    }

    // 表单提交处理
    document.getElementById('changePasswordForm').addEventListener('submit', function(e) {
        e.preventDefault();

        const oldPassword = document.getElementById('oldPassword').value;
        const newPassword = document.getElementById('newPassword').value;
        const confirmPassword = document.getElementById('confirmPassword').value;
        const submitBtn = document.getElementById('submitBtn');

        // 表单验证
        if (!oldPassword) {
            showMessage('请输入原密码', 'danger');
            return;
        }

        if (!newPassword) {
            showMessage('请输入新密码', 'danger');
            return;
        }

        if (newPassword.length < 6) {
            showMessage('新密码至少需要6个字符', 'danger');
            return;
        }

        if (newPassword !== confirmPassword) {
            showMessage('新密码和确认密码不一致', 'danger');
            return;
        }

        if (oldPassword === newPassword) {
            showMessage('新密码不能与原密码相同', 'danger');
            return;
        }

        // 禁用提交按钮
        submitBtn.disabled = true;
        submitBtn.innerHTML = '<i class="bi bi-hourglass-split me-2"></i>修改中...';

        // 发送AJAX请求
        fetch('${pageContext.request.contextPath}/student/changePassword', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: 'oldPassword=' + encodeURIComponent(oldPassword) + '&newPassword=' + encodeURIComponent(newPassword) + '&confirmPassword=' + encodeURIComponent(confirmPassword)
        })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    showMessage(data.message || '密码修改成功！', 'success');
                    // 清空表单
                    document.getElementById('changePasswordForm').reset();
                    // 3秒后跳转回首页
                    setTimeout(() => {
                        window.location.href = '${pageContext.request.contextPath}/student/home';
                    }, 2000);
                } else {
                    showMessage(data.message || '密码修改失败', 'danger');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                showMessage('修改密码时发生错误，请稍后重试', 'danger');
            })
            .finally(() => {
                // 恢复提交按钮
                submitBtn.disabled = false;
                submitBtn.innerHTML = '<i class="bi bi-check-lg me-2"></i>确认修改';
            });
    });

    // 密码强度检测
    document.getElementById('newPassword').addEventListener('input', function() {
        const password = this.value;
        const requirements = document.querySelector('.password-requirements');

        if (password.length >= 6) {
            requirements.style.borderLeft = '4px solid #28a745';
        } else {
            requirements.style.borderLeft = '4px solid #dc3545';
        }
    });

    // 确认密码实时验证
    document.getElementById('confirmPassword').addEventListener('input', function() {
        const newPassword = document.getElementById('newPassword').value;
        const confirmPassword = this.value;

        if (confirmPassword && newPassword !== confirmPassword) {
            this.setCustomValidity('密码不匹配');
            this.style.borderColor = '#dc3545';
        } else {
            this.setCustomValidity('');
            this.style.borderColor = '#28a745';
        }
    });

    // 显示个人信息模态框
    function showProfileModal() {
        // 关闭用户名下拉菜单
        const dropdown = document.getElementById('userDropdown');
        dropdown.classList.remove('show');

        // 显示模态框
        const modal = new bootstrap.Modal(document.getElementById('profileModal'));
        modal.show();
    }
</script>
</body>
</html>

